package net.pm.core.web.order;

import java.util.Date;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.Pager;
import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.dateEdtor.DateEditor_Long;
import net.pm.core.dateEdtor.DateEditor_Short;
import net.pm.core.entities.db.Orderinfo;
import net.pm.core.service.OrderServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *生产订单管理
 */
@Controller
@Scope("request")
@RequestMapping("/order/")
@SessionAttributes("order")
public class OrderController extends BaseController {
	/**
	 * 重定向到订单管理页面
	 */
	private static final String REDIRECT_ORDER_MANAGE = "redirect:/order/manage.htm";
	
	@Resource(name="DateEditor_Short")
	private DateEditor_Short dateEditor_Short;
	@Resource(name="DateEditor_Long")
	private DateEditor_Long dateEditor_Long;
	@Resource(name="OrderServicer")
	private OrderServicer orderServicer;

	
	/**
	 *(1) 创建生产订单,初始化页面,flow(流) 开始
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_ORDER)
	@RequestMapping(value="create",method=RequestMethod.GET)
	public String initOrder(final ModelMap modelMap) {
		modelMap.addAttribute("order", new Orderinfo());
		return "order/order_create";
	}
	
	/**
	 * (2)添加订单产品信息,
	 * @param order
	 * @return
	 */
	@RequestMapping(value="product/create",method=RequestMethod.POST)
	public String processOrderProduct(@ModelAttribute("order") Orderinfo order){
		return "order/product_create";
	}
	@RequestMapping(value="product/create",method=RequestMethod.GET)
	public String processOrderProduct(){
		return "order/product_create";
	}
	
	/**(3)添加订单客户信息
	 * @param order
	 * @return
	 */
	@RequestMapping(value="guest/create",method=RequestMethod.POST)
	public String processOrderGuest(@ModelAttribute("order") Orderinfo order){
		return "order/guest_create";
	}
	
	/**
	 * (4)存入数据库,flow(流) 结束
	 * @param order
	 * @param sessionStatus
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_ORDER)
	@RequestMapping(value="create/finish",method=RequestMethod.POST)
	public ModelAndView processOrderFinish(
			@ModelAttribute("order") Orderinfo order,
			SessionStatus sessionStatus) {
		order.setOrderUser(super.obtainCurrentUser());
		//存库
		orderServicer.createOrder(order);

		super.setSessionMessage("生产订单生成成功!");
		Long orderId = order.getId();

		// 清空session中的order对象, 此类类名处注解@SessionAttributes("order")
		sessionStatus.setComplete();
		return new ModelAndView("order/order_success").addObject("id", orderId);
	}
	
	/**
	 * 中途取消,flow结束
	 * @param sessionStatus
	 * @return
	 */
	@RequestMapping(value="create/cancel",method=RequestMethod.GET)
	public String processOrderCancel(SessionStatus sessionStatus){
		//清空session中的order对象,  此类类名处注解@SessionAttributes("order")
		sessionStatus.setComplete();
		return REDIRECT_ORDER_MANAGE;
		
	}
	
	
	/**
	 * 去到订单管理页面
	 * @return
	 */
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public ModelAndView manage() {
		
		ModelAndView mav = new ModelAndView("order/order_manage");
		mav.addObject("Orderinfo", super.useStaticPacker(Orderinfo.class.getName()));
		mav.addObject("statusesMap", OrderServicer.statusesMap);
		mav.addObject("this",super.useStaticPacker(this.getClass().getName()));
		return mav;
	}
	
	/**
	 * 自动转到默认页
	 * @return
	 * */
	@RequestMapping(value = "page", method = RequestMethod.GET)
	public ModelAndView page() {
		return page(DevConstant.PAGE_DEFAULT_SIZE,DevConstant.PAGE_DEFAULT_NO,null,null,Orderinfo.STATUS_DEFAULT_DISPLAY);
	}
	
	/**
	 *  订单管理页面
	 * @return
	 */
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ModelAndView page(Integer pagesize, Integer pageno, Date startdate,
			Date enddate, Short statusKey) {
		ModelAndView mav = new ModelAndView("order/order_page");
		Pager<Orderinfo> pager = orderServicer.findOrderPagerByOwner(statusKey,
				startdate, enddate, pagesize, pageno);
		mav.addObject("pager", pager);
		mav.addObject("this", super.useStaticPacker(this.getClass().getName()));
		mav.addObject("Orderinfo", super.useStaticPacker(Orderinfo.class.getName()));
		return mav;
	}
	
	/**
	 * 获取订单对应的状态的字符串
	 * @param status
	 * @return
	 */
	public static String obtainStatusStr(Short status){
		String[] statusStrs = {"等待生产","生产中","生产完成","生产失败","生产取消","默认显示","全部"};
		return statusStrs[status];
	}
	
	/**
	 * 订单详细信息
	 * @param orderId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.VIEW_ORDER)
	@RequestMapping(value="info",method=RequestMethod.GET)
	public ModelAndView info(Long orderId){
		ModelAndView mav = new ModelAndView("order/order_info");
		Orderinfo order = orderServicer.findEntityById(orderId);
		mav.addObject("order", order);
		mav.addObject("this", super.useStaticPacker(this.getClass().getName()));
		mav.addObject("Orderinfo", super.useStaticPacker(Orderinfo.class.getName()));
		return mav;
	}
	
	/**
	 * 投入生产
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.PRODUCE_ORDER)
	@RequestMapping(value="into/produce",method=RequestMethod.PUT)
	public String intoProduce(Long orderId){
		Long userId = super.obtainCurrentUser().getId();
		orderServicer.intoProduce(orderId,userId);
		super.setSessionMessage("订单状态己改变.");
		return REDIRECT_ORDER_MANAGE;
	}
	
	/**
	 * 取消订单
	 * @param orderId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CANCEL_ORDER)
	@RequestMapping(value="cancel",method=RequestMethod.PUT)
	public String  cancelOrder(Long orderId){
		orderServicer.cancelOrder(orderId);
		super.setSessionMessage("取消订单成功!");
		return REDIRECT_ORDER_MANAGE;
	}
	
	/**
	 * 检查订单是否可以取消　
	 * @param status
	 */
	public static Boolean isCanCancel(Short status){
		Short[] statues = new Short[]{Orderinfo.STATUS_WAIT,Orderinfo.STATUS_PRODUCE};
		for(Short s : statues){
			if(s.equals(status)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 删除订单
	 * @param orderId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.REMOVE_ORDER)
	@RequestMapping(value="remove",method=RequestMethod.PUT)
	public String removeOrder(Long orderId){
		orderServicer.removeOrder(orderId);
		super.setSessionMessage("删除订单成功!");
		return REDIRECT_ORDER_MANAGE;
	}
	
	/**
	 * 是否可以删除
	 * @param status
	 * @return
	 */
	public static Boolean isCanRemove(Short status){
		Short[] statues = new Short[]{Orderinfo.STATUS_FAILED,Orderinfo.STATUS_CANCEL};
		for(Short s : statues){
			if(s.equals(status)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 数据绑定
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, dateEditor_Short);
		binder.registerCustomEditor(Date.class, dateEditor_Long);
	}
	
	
}
